/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.dal;

import edu.uoc.tdp.entity.Resource;

import edu.uoc.tdp.customException.EIssueException;
import edu.uoc.tdp.entity.BookingResource;
import edu.uoc.tdp.i18n.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
  
/**
 * @author tomeu
 */
public class ManagementBookingResources {

   private ManagementDb mngBD = null;
   private Connection conexion = null;

    public ManagementBookingResources() throws EIssueException{
        try {
            mngBD = new ManagementDb();
            mngBD.connectDB();
            conexion = mngBD.getConnection();
        } catch (edu.uoc.tdp.common.EIssueException ex) {
            Logger.getLogger(ManagementBookingResources.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ManagementBookingResources(ManagementDb mngDb) {
        mngBD = mngDb;
        conexion = mngDb.getConnection();
    }

    /**
     * Mètode que obte la lista de reserves - taula booking_resources
     * @return Lista de recursos d'una reserva
     */
    public List<BookingResource> GetAllBookingResources() throws EIssueException{
        List<BookingResource> listBookingResources = new ArrayList();

       try {
            Statement command = this.mngBD.createCommand();
            String select = "SELECT bookingid, resourceid, record_date  FROM booking_resources";
            ResultSet rs = command.executeQuery(select);
            while (rs.next()) {
                listBookingResources.add(new BookingResource(rs.getInt(1), rs.getInt(3),rs.getDate(3) ));
                // El primer index en rs es el 1
            }
            rs.close();
            command.close();
        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());
            throw new EIssueException(TDPLanguageUtils.getMessage
                    (TDPLanguageConstants.ERR_SQL));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw new EIssueException(TDPLanguageUtils.getMessage
                    (TDPLanguageConstants.ERR_UNKNOW));
        }
        return listBookingResources;
    }

    
    /**
     * Mètode que obte tots els recursos d'una reserva - taula booking_resources
     * @return Lista de recursos d'una reserva
     */
    public List<Resource> GetOneBookingResources(int bookingid) throws EIssueException {
        List<Resource> listResources = new ArrayList();
        
        try {
            Statement command = this.mngBD.createCommand();
            String select = "SELECT BR.record_date, R.*  FROM booking_resources as BR  inner join all_resources as R on R.resourceid = BR.resourceid WHERE bookingid="+ bookingid +" ORDER BY BR.resourceid";
            ResultSet rs = command.executeQuery(select);
            while (rs.next()) {
                //Integer resourceid, String resourcedescription, Integer amount, Double price_hour, Double price_day, Date creation_date, String creation_user, Date inactivation_date, String inactivation_user, Date record_date) {
                int resourceId = rs.getInt("resourceId");
                String resourceDescription = rs.getString("resourcedescription");
                int amount =rs.getInt("amount");
                Double priceHour = rs.getDouble("price_hour");
                Double priceDay = rs.getDouble("price_day");
                Date creationDate = rs.getDate("creation_date");
                String creation_user  = rs.getString("creation_user");
                Date inactivacionDate = rs.getDate("inactivation_date");
                String inactivationUser = rs.getString("inactivation_user");
                Date recordDate = rs.getDate("record_date");
                listResources.add(new Resource(rs.getInt("resourceid"), rs.getString("resourcedescription"),rs.getInt("amount"),rs.getDouble("price_hour"),rs.getDouble("price_day"),rs.getDate("creation_date"),rs.getString("creation_user"),rs.getDate("inactivation_date"),rs.getString("inactivation_user"),rs.getDate("record_date")));
            }
            rs.close();
            command.close();

        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());
            throw new EIssueException(TDPLanguageUtils.getMessage
                    (TDPLanguageConstants.ERR_STOP));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw new EIssueException(TDPLanguageUtils.getMessage
                    (TDPLanguageConstants.ERR_UNKNOW));
        }
        
        return listResources;
    }    
        /**
     * Mètode que guarda dades (Insert) un recurs dins d'una aula - taula spaces_resources
     * @return boolea
     */    
    public boolean SaveBookingResource(int bookingid, int resourceId) throws EIssueException{
        boolean PROCESS_STATE = false;
        try {
            String insertQuery = "INSERT INTO booking_resources(bookingid, resourceid, record_date) VALUES (?, ?, now())";
            PreparedStatement command = this.mngBD.prepareCommand(insertQuery);
            command.setInt(1, bookingid);
            command.setInt(2, resourceId);
            command.execute();
            command.close();
            PROCESS_STATE = true;
        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());
            throw new EIssueException(TDPLanguageUtils.getMessage
                    (TDPLanguageConstants.ERR_SQL));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw new EIssueException(TDPLanguageUtils.getMessage
                    (TDPLanguageConstants.ERR_UNKNOW));
        }
        
        return PROCESS_STATE;
    }
}
